package com.ruitu.health_2.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.health_2.R;
import com.ruitu.health_2.ui.adapter.ModelArticleListAdapter;
import com.ruitu.router_module.Router;
import com.ruitu.router_module.bean.article.Article;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ArticleListFragment extends BaseFragment {
    private RecyclerView rcv_list;
    private RelativeLayout rl_empty;

    private static List<Article> articleList = new ArrayList<>();
    private ModelArticleListAdapter adapter;

    public static ArticleListFragment newInstance(List<Article> articleList) {
        ArticleListFragment.articleList = articleList;
        Bundle args = new Bundle();
        ArticleListFragment fragment = new ArticleListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        adapter = new ModelArticleListAdapter(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_article_list, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);
        rl_empty = v.findViewById(R.id.rl_empty);

        ((TextView) (rl_empty.findViewById(R.id.txt_description))).setText("暂时没有找到相关文章");

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        adapter.setDataList(articleList);
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", articleList.get(position).getId());
                try {
                    Router.startActivity(getActivity(), "com.ruitu.health_1.ui.ArticleDetailsActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        showOrHideEmptyView();

        return v;
    }

    protected void showOrHideEmptyView() {
        if (isDataEmpty(articleList)) {
            rl_empty.setVisibility(View.VISIBLE);
        } else {
            rl_empty.setVisibility(View.GONE);
        }
    }
}
